package list;

/**题目：
 * 合并两个有序的链表
 * @Author Gavin
 * @date 2021.12.19 21:51
 */
public class list_3 {
    //Time:O(m+n) Space:O(1)
    public static ListNode solution(ListNode l1,ListNode l2){
        ListNode p=new ListNode(),pCurr=p;
        while (l1!=null&&l2!=null){
            if(l1.val<l2.val){
                pCurr.next=l1;
                l1=l1.next;
            }else{
                pCurr.next=l2.next;
                l2=l2.next;
            }
            pCurr=pCurr.next;
        }
        if(l1!=null)pCurr.next=l1;
        if(l2!=null)pCurr.next=l2;
        return p.next;
    }
}
